package com.ruoyi.djb.interceptor;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.ruoyi.djb.config.Constants.SYS_STATUS_DELETE;
import static com.ruoyi.djb.config.Constants.SYS_STATUS_DISSOLUTION;

/**
 * @author jinbin
 * @date 2018-07-08 22:37
 */
@ControllerAdvice
public class GloablExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        String msg = e.getMessage();
        int code = 500;
        if (msg == null || msg.equals("")) {
            msg = "服务器出错";
        }else if(msg.contains("重新登录")){
            code=501;
        }else if(msg.contains(SYS_STATUS_DELETE)){
            code=601;
            msg=msg.substring(msg.indexOf(":")+1);
        }else if(msg.contains(SYS_STATUS_DISSOLUTION)){
            code=602;
            msg=msg.substring(msg.indexOf(":")+1);
        }else if(msg.contains("该访客信息已登记")){
            code=606;
            msg=msg.substring(msg.indexOf(":")+1);
        }else if(msg.contains("操作频繁")){
            code=701;
        }
        System.out.println(e.getMessage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg", msg);
        jsonObject.put("code",code);
        return jsonObject;
    }
}
